﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class ReturnToFactoryConfirmQuotation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        #region 初始化页面数据
        private void InitialPage()
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            ReturnToFactoryForm form = service.GetReturnToFactoryFormProductDetail(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));

            ProviderService providerservice = new ProviderService();
            Provider provider = providerservice.GetProviderByCode(form.ProvideCode);

            if (provider != null)
            {
                providerNameLabel.Text = provider.ProviderName;
                providerCodeLabel.Text = provider.ProviderCode;
                connectPersonLabel.Text = provider.Contact1Name + "," + provider.Contact2Name;
                addressLabel.Text = provider.Address;
                phoneLabel.Text = provider.Contact1Phone + "," + provider.Contact2Phone;
                emailLabel.Text = provider.Contact1Email + "," + provider.Contact2Email;
            }

            runningNumberLabel.Text = form.RunningNumber;
            facotryRMALabel.Text = form.Code;

            rmaCodeLabel.Text = form.ApplicationProduct.ApplicationForm.ApplicationCode;
            applicantEmailLabel.Text = form.ApplicationProduct.ApplicationForm.User.UserEmail;
            applicantLabel.Text = form.ApplicationProduct.ApplicationForm.User.FullName;
            applicantPhoneLabel.Text = form.ApplicationProduct.ApplicationForm.User.UserPhone;

            rtfDateTimeLabel.Text = form.ApplyDate.Value.ToString("yyyy-MM-dd");
            productTypeLabel.Text = form.ApplicationProduct.ProductType.ProductTypeName;
            versionLabel.Text = form.ApplicationProduct.ProductVersionNumber;
            productSerialNumberLabel.Text = form.ApplicationProduct.ProductSerialNumber;
            problemDescriptionLabel.Text = form.ApplicationProduct.ProductProblemDescription;
            reasonLabel.Text = form.ApplicationProduct.ReasonOfCost;
            confirmQuatationTextBox.Text = DateTime.Now.ToString("yyyy-MM-dd");

            quotationDateLabel.Text = form.ProviderQuatationDate.HasValue? form.ProviderQuatationDate.Value.ToString("yyyy-MM-dd"):"";
            customerQuatationDateLbl.Text = form.ApplicationProduct.QuotationDateTime.HasValue ? form.ApplicationProduct.QuotationDateTime.Value.ToString("yyyy-MM-dd") : "";
            customerQuatationReasonLbl.Text = form.ApplicationProduct.ReasonOfCost;
            customerQuationConfirmDateLbl.Text = form.ApplicationProduct.ConfirmQuotationDateTime.HasValue ? form.ApplicationProduct.ConfirmQuotationDateTime.Value.ToString("yyyy-MM-dd") : "";
       
            if (form.ApplicationProduct.IsDecideToRepair.HasValue && !form.ApplicationProduct.IsDecideToRepair.Value)
            {
                customerDecisionLbl.Text = "不维修";
            }
            else
            {
                customerDecisionLbl.Text = "确认维修";
            }
            BindCustomerQuatationList();
            BindProviderQuatationList();
        }

        private void BindCustomerQuatationList()
        {
            ApplicationProductService productService = new ApplicationProductService();
            ReturnToFactoryService returnFactoryService = new ReturnToFactoryService();
            ReturnToFactoryForm returnFactoryForm = returnFactoryService.GetReturnToFactoryFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
            if (!returnFactoryForm.ApplicationProductReference.IsLoaded)
            {
                returnFactoryForm.ApplicationProductReference.Load();
            }
            List<QuatationForm> forms = productService.GetQuatationFormByProductID(returnFactoryForm.ApplicationProduct.ApplicationProductID);
            string accessorys = "";
            double price = 0;
            foreach (QuatationForm form in forms)
            {
                accessorys += form.AccessoryName + ",";
                price += form.Cost;
            }
            customerQuatationAccessoryPrice.Text = "￥：" + price.ToString() + "元";
            customerQuatationAccessoryLbl.Text = accessorys;
            if (!string.IsNullOrEmpty(accessorys))
            {
                customerQuatationAccessoryLbl.Text = accessorys.Substring(0, accessorys.Length - 1);
            }
        }

        private void BindProviderQuatationList()
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            IQueryable<ReturnToFactoryQuatation> forms = service.GetQuatationList(Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
            string accessorys = "";
            decimal price = 0;
            foreach (ReturnToFactoryQuatation form in forms)
            {
                accessorys += form.AccessoryName + ",";
                price += form.Count * form.UnitPrice;
            }
            factoryQuatationPriceLbl.Text = "￥：" + Math.Round(price, 2).ToString() + "元";
            factoryQuataionAccessoryLbl.Text = accessorys;
            if (!string.IsNullOrEmpty(accessorys))
            {
                factoryQuataionAccessoryLbl.Text = accessorys.Substring(0, accessorys.Length - 1);
            }
        }
        #endregion

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                ReturnToFactoryService service = new ReturnToFactoryService();
                ReturnToFactoryForm form = service.GetReturnToFactoryFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }


                string tip = string.Empty;
                if (isRepairDropDownList.Text.Equals("是"))
                {
                    form.DoesRepair = true;
                    form.Status = "返厂-待收货确认";
                    form.ConfirmQuatationDate = DateTime.Now;
                    tip = "已成功确认报价";
                }
                else if (isRepairDropDownList.Text.Equals("否"))
                {
                    form.DoesRepair = false;
                    form.Status = "返厂-待收货确认";
                    tip = "确认为不让工厂维修";
                }
                service.Update(form);
                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('" + tip + "');location.href='ReturnToFactoryListConfirmQuotation.aspx';", true);
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ReturnToFactoryConfirmQuotation.aspx::saveLinkButton_Click");
                throw ex;
            }
        }        
    }
}
